package com.startest.sms.service;


import com.startest.sms.dao.TransactionMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
public class TransactionServiceImpl implements TransactionService {

    private static final  Logger logger = LoggerFactory.getLogger(TransactionServiceImpl.class);

    @Autowired
    private TransactionMapper transactionMapper;


    /*jdbc事务，默认只会回滚运行时异常，如果异常抛出，将捕捉不到异常，
    实际开发中常在catch中手动抛出异常*/
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout = 20000,rollbackFor = {RuntimeException.class,Exception.class})
    @Override
    public void transfer(Integer out, Integer in, Integer money) {
        transactionMapper.outM(out,money);
        try {
            int temp = 4/0;
        } catch (Exception e) {
            logger.error("error严重错误",e);
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        transactionMapper.inM(in,money);
    }


}
